/*
 * A unicastremote class for RemoteSpelling, fufill the method of interface.
 * Written by YUE CHI
 */

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.SortedSet;

/**
 * A unicastremote class for RemoteSpelling, fufill the method of interface.
 * @author YUE CHI
 * @version 1.0
 */
public class UnicastRemoteSpelling implements RemoteSpelling
{
  /**
   * The wordlist store on the server.
   */
  private final WordList my_wordlist;

  /**
   * The constructor.
   * @param the_args  the input String for wordlist name and location.
   * @throws IOException  io problems
   */
  public UnicastRemoteSpelling(final String the_args) throws IOException
  {
    this.my_wordlist = new WordList(the_args);
  }

  /**
   * The check function that implement the interface.
   * @param the_word input word need to be check.
   * @return A Sortedset of suggestion word.
   * @throws RemoteException   if there is a problem completing the method call.   
   */
  public SortedSet<String> check(final String the_word) throws RemoteException
  {
    Log.out("Received query for word: " + the_word);
    SortedSet<String> tempset = null;
    if (my_wordlist.isInList(the_word))
    {
      Log.out(the_word + " is spelled correctly");
    }
    else
    {
      tempset = my_wordlist.getCloseWords(the_word);
      if (tempset.size() == 0)
      {
        Log.out(the_word + " is spelled incorrecly. There is no suggestions.");
      }
      else
      {
        final Iterator<String> suggestions = tempset.iterator();
        final StringBuffer sb = new StringBuffer();
        while (suggestions.hasNext())
        {
          final String temp = suggestions.next();
          sb.append(' ');
          sb.append(temp);
        }
        Log.out(the_word + " is spelled incorrecly. There is " + tempset.size() +
                " suggestions:" + sb.toString());

      }
    }
    return tempset;
  }

  /**
   * Add the specified word to the word list. If the word is already in
   * the word list, this method has no effect.
   * 
   * @param the_word The word to add.
   * @throws RemoteException if there is a problem completing the method call.
   */
  public void add(final String the_word) throws RemoteException
  {
    if (this.my_wordlist.add(the_word))
    {
      Log.out(the_word + " has been added to the wordlist");
    }
    else
    {
      Log.out(the_word + " is already in the list. No need to add");
    }

  }
  /**
   * Remove the specified word from the word list. If the word is not
   * in the word list, this method has no effect.
   * 
   * @param the_word The word to remove.
   * @throws RemoteException if there is a problem completing the method call.
   */
  public void remove(final String the_word) throws RemoteException
  {
    if (this.my_wordlist.remove(the_word))
    {
      Log.out(the_word + " has been removed from the wordlist");
    }
    else
    {
      Log.out(the_word + " is not in the list. Cannot be removed");
    }

  }

}
